package handler

import (
	"encoding/json"
	"fmt"
	"github.com/gocolly/colly"
	"sbd_zgqx/models"
)

func ReListPageSizeHandler() (pageSize int) {
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))

	c.OnRequest(func(r *colly.Request) {
		// r.Headers.Set("Cookie", "JSESSIONID=A87C2BE548AF94B7CC4B9FA8E40178D0; pageSize=20; pageNo=1")
		// r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")

	})
	c.OnResponse(func(r *colly.Response) {
		var f interface{}
		json.Unmarshal(r.Body, &f)
		pageSize = int(f.(map[string]interface{})["total"].(float64))

	})
	c.Visit("http://202.127.42.47:6010/VarietyRegistrationListHandler.ashx?interfacetype=pzdj_list&RegistrationNo=&CropName=&VarietyName=&AcceptTime=&ApplyName=&RegionId=999999&_search=false&nd=1662446724381&rows=15&page=1&sidx=DJYear&sord=desc")

	return
}

func ReListHandler() (isBreeders models.Province, err error) {
	var result Result
	// isBreeder := models.BreederNo{}

	urlProvince := fmt.Sprintf("https://weather.cma.cn/api/dict/province")
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))

	c.OnRequest(func(r *colly.Request) {
		// r.Headers.Set("Cookie", "JSESSIONID=A87C2BE548AF94B7CC4B9FA8E40178D0; pageSize=20; pageNo=1")
		// r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")

	})
	c.OnResponse(func(r *colly.Response) {
		err := json.Unmarshal(r.Body, &result)
		// fmt.Println(string(r.Body))
		if err != nil {
			fmt.Println("aaaa ====>", err)
		}
		fmt.Println(result.Data)
		// for _, v := range result.Rows {
		// 	v.ID = utils.SnowId()
		// 	v, err = ReListInfoHandler(v)
		// 	if err != nil {
		// 		fmt.Println("bbbb ====>", err)
		// 	}
		// 	isBreeders = append(isBreeders, v)
		// }

	})
	c.Visit(urlProvince)
	return
}

//
// type RowResutl []struct {
// 	RowNumber             int         `json:"RowNumber"`
// 	RegistrationNo        string      `json:"RegistrationNo"`
// 	CropName              string      `json:"CropName"`
// 	VarietyName           string      `json:"VarietyName"`
// 	DJYear                interface{} `json:"DJYear"`
// 	SHDW                  interface{} `json:"SHDW"`
// 	ApplyName             string      `json:"ApplyName"`
// 	IsTransgenosis        interface{} `json:"IsTransgenosis"`
// 	VarietySource         string      `json:"VarietySource"`
// 	VarietyFeature        string      `json:"VarietyFeature"`
// 	SuitableAreaAndSeason string      `json:"SuitableAreaAndSeason"`
// 	Yield                 string      `json:"Yield"`
// 	Quality               string      `json:"Quality"`
// 	Resistance            string      `json:"Resistance"`
// 	Status                interface{} `json:"Status"`
// 	BreederName           string      `json:"BreederName"`
// 	CultureTechnique      string      `json:"CultureTechnique"`
// 	Attentions            string      `json:"Attentions"`
// 	IsAnnouncement        interface{} `json:"IsAnnouncement"`
// 	VarietyHasLincense    interface{} `json:"VarietyHasLincense"`
// 	HasGrant              interface{} `json:"HasGrant"`
// 	HasPromotion          interface{} `json:"HasPromotion"`
// 	HasFilling            interface{} `json:"HasFilling"`
// }
//
// func ReListInfoHandler(isRegistration models.Registration) (regist models.Registration, err error) {
// 	fmt.Println("first registration :\n", isRegistration)
//
// 	var RowResult RowResutl
//
// 	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))
//
// 	url := fmt.Sprintf("http://202.127.42.47:6010/VarietyRegistrationListHandler.ashx")
//
// 	data := map[string]string{
// 		"RegistrationNo": isRegistration.RegistrationNo,
// 		"interfacetype":  "pzdj_getAnnouncementinfo",
// 	}
//
// 	c.OnRequest(func(r *colly.Request) {
// 		r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")
//
// 	})
//
// 	c.OnResponse(func(r *colly.Response) {
// 		err := json.Unmarshal(r.Body, &RowResult)
// 		if err != nil {
// 			fmt.Println(err)
// 		}
// 		for k, v := range RowResult {
// 			fmt.Println("RowResult key's ============> ", k)
// 			result, err := json.Marshal(v)
// 			if err != nil {
// 				fmt.Println("json.Marshal to other struct err : ", err)
// 			}
// 			err = json.Unmarshal(result, &isRegistration)
// 			if err != nil {
// 				fmt.Println("json.Unmarshal to isRegistration struct err : ", err)
// 			}
// 			regist = isRegistration
//
// 			fmt.Println("first registration :\n", regist)
// 		}
//
// 	})
// 	c.Post(url, data)
// 	return
// }
